package bb.lanxing.util.text;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
import com.umeng.socialize.net.utils.SocializeProtocolConstants;
import gov.nist.core.Separators;
import bb.lanxing.activity.deeplink.DeepLinkPathConstants;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class UriParse {
    public static String getPath(final Context context, final Uri uri) {
        Uri uri2 = null;
        if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context, uri)) {
            if (isExternalStorageDocument(uri)) {
                String[] split = DocumentsContract.getDocumentId(uri).split(Separators.COLON);
                if ("primary".equalsIgnoreCase(split[0])) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
            } else if (isDownloadsDocument(uri)) {
                return getDataColumn(context, ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                        Long.parseLong(DocumentsContract.getDocumentId(uri))), null, null);
            } else {
                if (isMediaDocument(uri)) {
                    String[] split2 = DocumentsContract.getDocumentId(uri).split(Separators.COLON);
                    String str = split2[0];
                    if (SocializeProtocolConstants.IMAGE.equals(str)) {
                        uri2 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    } else if ("video".equals(str)) {
                        uri2 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    } else if ("audio".equals(str)) {
                        uri2 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    }
                    return getDataColumn(context, uri2, "_id=?", new String[]{split2[1]});
                }
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            if (isGooglePhotosUri(uri)) {
                return uri.getLastPathSegment();
            }
            return getDataColumn(context, uri, null, null);
        } else if (DeepLinkPathConstants.SCHEME_FILE.equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }

    public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
        Cursor cursor = null;
        try {
            Cursor query = context.getContentResolver().query(uri, new String[]{"_data"}, selection, selectionArgs, null);
            if (query != null) {
                try {
                    if (query.moveToFirst()) {
                        String string = query.getString(query.getColumnIndexOrThrow("_data"));
                        query.close();
                        return string;
                    }
                } catch (Throwable th) {
                    cursor = query;
                    cursor.close();
                    throw th;
                }
            }
            if (query != null) {
                query.close();
            }
            return null;
        } catch (Throwable th2) {
            th2.printStackTrace();
        }

        return null;

    }

    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    public static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }

    public static String getFileName(Context context, Uri uri) {
        String str;
        if (context.getContentResolver().getType(uri) == null) {
            String path = getPath(context, uri);
            if (path == null) {
                str = getName(uri.toString());
            } else {
                str = new File(path).getName();
            }
        } else {
            Cursor query = context.getContentResolver().query(uri, null, null, null, null);
            if (query != null) {
                int columnIndex = query.getColumnIndex("_display_name");
                query.moveToFirst();
                String string = query.getString(columnIndex);
                query.close();
                str = string;
            } else {
                str = null;
            }
        }
        Log.d("UriParse", "getFileName:" + str);
        return str;
    }

    public static String getName(String filename) {
        if (filename == null) {
            return null;
        }
        return filename.substring(filename.lastIndexOf(47) + 1);
    }

    public static void saveFileFromUri(Context context, Uri uri, String destinationPath) {
        BufferedOutputStream bufferedOutputStream;
        InputStream inputStream = null;
        try {
            InputStream openInputStream = context.getContentResolver().openInputStream(uri);
            try {
                bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destinationPath, false));

                try {
                    byte[] bArr = new byte[1024];
                    openInputStream.read(bArr);
                    do {
                        bufferedOutputStream.write(bArr);
                    } while (openInputStream.read(bArr) != -1);
                    openInputStream.close();
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    inputStream = openInputStream;
                    try {
                        e.printStackTrace();
                        inputStream.close();
                        bufferedOutputStream.close();
                    } catch (Throwable th) {
                        try {
                            inputStream.close();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                            throw th;
                        }
                        bufferedOutputStream.close();
                        throw th;
                    }
                }
            } catch (Throwable th3) {
                th3.printStackTrace();
            }
        } catch (Throwable th4) {
            th4.printStackTrace();
        }
    }
}
